﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour 
{
	public GameObject hazard;
	public Vector3 spawnValues;
	public int hazardCount;
	public float spawnWait;
	public float startWait;
	public float waveWait;

	public GUIText restartText;
	public GUIText gameOverText;
	public GUIText scoreText;

	private bool gameOver;
	private bool restart;

	private int score;
	private float speed;

	void Start(){

		gameOver = false;
		restart = false;

		restartText.text = "";
		gameOverText.text = "";

		score = 0;
		speed = 0;
		UpdateScore ();

		StartCoroutine( SpawnWaves ());
	}

	void Update()
	{
		if (restart) 
		{
			if (Input.GetKeyDown (KeyCode.R)) {

				SceneManager.LoadScene("Main");
			}
		}	
	}
		

	IEnumerator SpawnWaves()
	{
		yield return new WaitForSeconds (startWait);

	
		while(true){
		for (int i = 0 ; i < hazardCount; i++)
		{
			Vector3 spawnPosition = new Vector3 (Random.Range(-spawnValues.x,spawnValues.x),spawnValues.y, spawnValues.z);
			Quaternion spawnRotation = Quaternion.identity;
			Instantiate (hazard, spawnPosition, spawnRotation);
			yield return new WaitForSeconds (spawnWait);
		}

			yield return new WaitForSeconds(waveWait);

			if (gameOver) 
			{
				restartText.text = "Press 'R' for ResStare";

				restart = true;

				break;
			}
	    }
	}


	void UpdateScore()
	{
		scoreText.text = "Score: " + score;

	}

	public void AddScore (int newScoreValue)
	{
		score += newScoreValue;

		UpdateScore ();

	}

	public void GameOver()
	{
		gameOverText.text = "Game Over!";

		gameOver = true;
			
	}
}
